home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / bbs_door / jcal003.zip / JCAL.C < prev    next >
C/C++ Source or Header  |  1996-01-09  |  6KB  |  331 lines

  1. /************************************************************************
  2.  * jcal.c Calander program for Science Club BBS in OMAN
  3.  *  SCIENCE CLUB BBS +968 593-728  28800 USR Sposter
  4.  * - FREEWARE - no rights reserved
  5.  *
  6.  * Can be compiled with Watcom : wcl -bt=dos -l=dos jcal.c getopt.c
  7.  * or with gcc on Linux: cc -O -s -o jcal jcal.c
  8.  *
  9.  ************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <getopt.h>
  16. #ifndef __unix__
  17.  extern char *optarg;
  18.  extern int optind;
  19. #endif
  20.  
  21. #define FEB 1
  22.  
  23. #define TC    209
  24. #define BC    207
  25. #define CC    197
  26. #define VBAR  179
  27. #define HBAR  196
  28. #define LVB   199
  29. #define RVB   182
  30. #define DHBAR 205
  31. #define DVBAR 186
  32. #define DLTC  201
  33. #define DRTC  187
  34. #define DLBC  200
  35. #define DRBC  188
  36.  
  37. #define TWID 34
  38.  
  39. short BG=7;
  40. short FG=4;
  41. short XG=1;
  42. short CS=22;
  43.  
  44. static void newline (void)
  45. {
  46. #ifdef __unix__
  47.     fputs("\033E", stdout);
  48. #else
  49.     fputs("\033[0m\033[0K\n", stdout);
  50. #endif
  51. }
  52.  
  53. static void SetCols(void)
  54. {
  55.     printf("\033[0;4%d;3%dm", BG, FG);            
  56. }
  57.  
  58. void pad(short n, char fill, short flag)
  59. {
  60.     if(flag & 1)
  61.     {
  62.     printf("\033[%d;3%dm", (FG & 8) >> 3, FG & 7);
  63.     }
  64.     
  65.     while(n--)
  66.     {
  67.          putchar(' ');
  68.     }
  69.  
  70.     if(flag & 2)
  71.     {
  72.     printf ("\033[4%dm", BG);
  73.     }
  74.  
  75.     if(fill == '\n')
  76.     {
  77.     newline();
  78.     }
  79.     else if(fill)
  80.     {
  81.          putchar(fill);
  82.     }
  83. }
  84.  
  85.  
  86. void Divide(char left, char mid, char div, char right)
  87. {
  88.     short k;
  89.     short l;
  90.     
  91.     pad(CS, left, 3);
  92.  
  93.     l = 79 - CS*2;
  94.     
  95.     for(k = 1; k < l; k++)
  96.     {
  97.     if((k % 5) == 0)
  98.     {
  99.         putchar(div);
  100.     }
  101.     else
  102.     {
  103.         putchar(mid);
  104.     }
  105.     }
  106.     putchar(right);
  107.     pad (0, '\n', 1);
  108. }
  109.  
  110. int main(int ac, char **av)
  111. {
  112.     char buf[80];
  113.     time_t t;
  114.     struct tm *tm;
  115.     auto const char daysof[]={31,28,31,30,31,30,
  116.                      31,31,30,31,30,31
  117.                  };
  118.     short leap;
  119.     short lday;
  120.     short first,i,j;
  121.     char *efile = NULL;
  122.     char *epilogue = NULL;
  123.     char *prolog = NULL;    
  124.     int c;
  125.  
  126.     while ((c = getopt(ac, av, "f:b:x:p:e:")) != EOF)
  127.     {
  128.     switch (c)
  129.       {
  130.       case 'c':
  131.         FG = atoi(optarg);
  132.         break;
  133.       case 'b':
  134.         BG = atoi(optarg);
  135.         break;
  136.       case 'x':
  137.         XG = atoi(optarg);
  138.         break;
  139.       case 'p':
  140.         prolog = strdup(optarg);
  141.         break;
  142.       case 'e':
  143.         epilogue = strdup(optarg);
  144.         break;
  145.       }
  146.     }        
  147.  
  148.     if(optind < ac)
  149.     {
  150.       efile = strdup(*(av+optind));
  151.     }
  152.     
  153.     t = time(NULL);
  154.  
  155.     tm = localtime(&t);
  156.     leap = (tm->tm_year % 4 == 0);         /* 2000 is a leap year ! */
  157.     strftime(buf, sizeof(buf), "%B %Y", tm);
  158.  
  159.     if(prolog)
  160.     {
  161.     fputs(prolog, stdout);
  162.     free(prolog);
  163.     }
  164.     putchar('\n');
  165.     SetCols();    
  166.     Divide(DLTC, DHBAR, DHBAR, DRTC);
  167.  
  168.     j = strlen(buf);
  169.     i = (34 - j)/2;
  170.     pad(CS, DVBAR, 3);
  171.     pad(i, 0, 0);
  172.     
  173.     printf("\033[30m%*s\033[3%dm", 
  174.        j, buf, FG);
  175.     pad(34-i-j, DVBAR, 2);
  176.     putchar ('\n');
  177.  
  178.     Divide(204, DHBAR, DHBAR, 185);
  179.     pad (CS, DVBAR, 3);
  180.     printf("\033[30m Su   Mo   Tu   We   Th   Fr   Sa \033[3%dm",
  181.        FG);
  182.     putchar(DVBAR);
  183.     pad(0, '\n', 1);
  184.     Divide(LVB, HBAR, 194, RVB);    
  185.     lday = daysof[tm->tm_mon];
  186.     if(tm->tm_mon == FEB)
  187.     {
  188.     lday += leap;
  189.     } 
  190.     first = tm->tm_wday + 1 - (tm->tm_mday % 7);
  191.     if(first < 0) first += 7;
  192.  
  193.     first = first % 7;
  194.  
  195.     pad(CS, DVBAR, 3);
  196.  
  197.     for(j = i = 0; ; i++)
  198.     {
  199.     if(i < first)
  200.     {
  201.         printf ("    %c", VBAR);
  202.     }
  203.     else
  204.     {
  205.         if(j < lday)
  206.         {
  207.         j++;
  208.         if(j == tm->tm_mday)
  209.         {
  210.             printf("\x1b[1;3%dm", XG);
  211.         }
  212.         else
  213.         {
  214.             fputs("\x1b[1;30m", stdout);
  215.         }
  216.         
  217.         printf(" %2d ", j);
  218.         SetCols();
  219.         }
  220.         else 
  221.         {
  222.         fputs("    ", stdout);
  223.         }
  224.         
  225.         if(((i+1) % 7) == 0)
  226.         {
  227.         putchar(DVBAR);
  228.                 putchar ('\n');
  229.  
  230.         if(j < lday)
  231.         {
  232.             Divide(LVB, HBAR, CC, RVB);
  233.             pad (CS, DVBAR, 3);
  234.         }
  235.         else
  236.         {
  237.             Divide(DLBC, DHBAR, BC, DRBC);
  238.             break;
  239.         }
  240.         }
  241.         else
  242.         {
  243.         putchar(VBAR);
  244.         }
  245.     }
  246.     }
  247.  
  248.     if(efile)
  249.     {
  250.     FILE *fp;
  251.     putchar ('\n');
  252.     if(fp = fopen(efile, "r"))
  253.     {
  254.         short nl = 0;
  255.         short mx = 0;
  256.         char txt[82];
  257.         while(fgets(txt, sizeof(txt), fp))
  258.         {
  259.         char *p;
  260.         short n;
  261.         
  262.         if(p = strchr(txt, '\n'))
  263.         {
  264.             *p = '\0';
  265.         }
  266.         n = strlen(txt);
  267.         if(n)
  268.         {
  269.             nl++;
  270.             if(n > mx)
  271.             {
  272.             mx = n;
  273.             }
  274.         }
  275.         }
  276.         if(nl)
  277.         {
  278.         short sfg;
  279.         
  280.                 pad(32, 0, 3);
  281.         fputs (" Today's Events ", stdout);
  282.         pad(0, '\n', 1);
  283.         rewind(fp);
  284.         mx = (mx + 3) & ~1;
  285.         CS = 39 - (mx >> 1);
  286.         sfg = FG;
  287.         FG = 15;
  288.         Divide(218, 196, 196, 191);
  289.         FG = sfg;
  290.         
  291.         printf("\033[0;4%dm", BG);
  292.         while(fgets(txt, sizeof(txt), fp))
  293.         {
  294.             char *p;
  295.             if(p = strchr(txt, '\n'))
  296.             {
  297.             *p = '\0';
  298.             }
  299.             if(strlen(txt))
  300.             {
  301.             pad(CS, 0, 3);
  302.             printf("\033[1;37;4%dm\263\033[0;30;4%dm ", 
  303.                    BG, BG);
  304.             printf("%-*.*s \263", mx-2, mx-2, txt);
  305.             pad(0, '\n', 1);
  306.             }
  307.         }
  308.         sfg = FG;
  309.         FG = 8;
  310.         Divide(192, 196, 196, 217);
  311.         FG = sfg;
  312.         }
  313.         fclose(fp);
  314.         free(efile);
  315.     }
  316.     }
  317.     puts("\033[0m");
  318.  
  319.     if(epilogue)
  320.     {
  321.     fputs(epilogue, stdout);
  322.     free(epilogue);
  323.     putchar('\n');
  324.     }
  325.     
  326.     return 0;    
  327. }
  328.  
  329.  
  330.  
  331.